package com.woniu.user.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.dto.IdentityInformationDto;
import com.woniu.exception.user.UserException;
import com.woniu.form.NameLoginForm;
import com.woniu.meals.client.MealsFeignClient;
import com.woniu.param.*;
import com.woniu.server.RedisKey;
import com.woniu.server.RedisUtil;
import com.woniu.user.entity.*;
import com.woniu.user.mapper.*;
import com.woniu.user.service.*;

import com.woniu.utils.MD5Util;
import com.woniu.utils.SMSUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * <p>
 * 登录表 服务实现类
 * </p>
 *
 * @author ly
 * @since 2024年12月05日
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Resource
    RedisUtil redisUtil;

    @Resource
    UserRoleService userRoleService;

    @Resource
    UserMapper userMapper;

    @Resource
    RoleMapper roleMapper;
    @Resource
    AdminMapper adminMapper;
    @Resource
    RoleService roleService;

    @Resource
    UserRoleMapper userRoleMapper;
    @Resource
    UserInfoMapper userInfoMapper;

    @Resource
    AdminService adminService;

    @Resource
    UserLoginLogsService userLoginLogsService;

    @Resource
    UserInfoService userInfoService;

    @Resource
    AreaCodeService areaCodeService;

    @Resource
    EmployeeInfoService employeeInfoService;

    @Resource
    MealsFeignClient mealsFeignClient;

    @Resource
    PermissionService permissionService;
    @Override
    public IdentityInformationDto loginFood(LoginParam loginParam) throws Exception {
        Integer userId = null;
        String loginStatus = "登录成功";
        IdentityInformationDto identityInformationDto = new IdentityInformationDto();
        //判断手机号是否存在
        User user = userMapper.selectOne(new QueryWrapper<User>().eq("user_phone", loginParam.getPhone()));
        if (user == null) {
            loginStatus = "登录失败 手机号不存在";
            throw new UserException(loginStatus, 10001);
        }
        //检验密码

        if (!user.getUserPassword().equals(MD5Util.md5(loginParam.getPassWord(), loginParam.getPhone()))) {
            loginStatus = "登录失败 密码错误";
            throw new UserException(loginStatus, 10002);
        }
        //获取角色
        userId = user.getUserId().intValue();
        UserRole userRole = userRoleMapper.selectRoleByUserIdWithoutRoleIdThree(userId);
        Role role = roleService.getById(userRole.getRoleId());

        if (role.getRoleId().equals(2)) {
            if (mealsFeignClient.selectStoreByUserId(userId).getData() == null) {
                identityInformationDto.setStoreId(null);
            } else {
                identityInformationDto.setStoreId(String.valueOf(mealsFeignClient.selectStoreByUserId(userId).getData().getStoreId()));
            }

        }

        //签发token
        user.setUserPassword("******");
        IdentityInformationDto.User user2 = BeanUtil.toBean(user, IdentityInformationDto.User.class);
        user2.setUserPhone(user.getUserPhone());
        identityInformationDto.setUser(user2);
        identityInformationDto.setUserName(user.getUserName());
        identityInformationDto.setRoleName(role.getRoleName());

        IdentityInformationDto.User user1 = identityInformationDto.getUser();
        user1.setRoleId(userRole.getRoleId());
        user1.setRoleName(role.getRoleName());
        identityInformationDto.setUser(user1);


        UserLoginLogs userLoginLogs = new UserLoginLogs();
        userLoginLogs.setUserId(userId.toString());
        userLoginLogs.setLoginTime(LocalDateTime.now());
        userLoginLogs.setUserName(user.getUserName());
        userLoginLogs.setLoginStatus(loginStatus);
        userLoginLogsService.save(userLoginLogs);

        permissionService.selectUserAll(userId.toString());

        return identityInformationDto;
    }

    @Override
    public IdentityInformationDto loginRequire(LoginParam loginParam) throws Exception {
        Integer adminId = null;
        String loginStatus = new String();
        IdentityInformationDto identityInformationDto = new IdentityInformationDto();
        //判断用户名是否存在
        Admin admin = adminMapper.selectOne(new QueryWrapper<Admin>().eq("phone", loginParam.getPhone()));

        //检验密码
        if (!admin.getAdminPassword().equals(MD5Util.md5(loginParam.getPassWord(), loginParam.getPhone()))) {
            loginStatus = "登录失败 密码错误";
            throw new UserException(loginStatus, 40002);
        }

        //获取角色
        adminId = admin.getAdminId().intValue();
        UserRole userRole = userRoleMapper.selectRoleByUserIdWithoutNormal(adminId);
        Role role = roleService.getById(userRole.getRoleId());


        //签发token
        admin.setAdminPassword("******");

        identityInformationDto.setUserName(admin.getAdminName());

        identityInformationDto.setRoleName(role.getRoleName());

        IdentityInformationDto.Admin admin1 = BeanUtil.toBean(admin, IdentityInformationDto.Admin.class);
        admin1.setRoleId(role.getRoleId());
        admin1.setRoleName(role.getRoleName());
        identityInformationDto.setAdmin(admin1);


        UserLoginLogs userLoginLogs = new UserLoginLogs();
        userLoginLogs.setUserId(admin.getAdminId().toString());
        userLoginLogs.setLoginTime(LocalDateTime.now());
        userLoginLogs.setUserName(admin.getAdminName());
        userLoginLogs.setLoginStatus(loginStatus);
        userLoginLogsService.save(userLoginLogs);

        System.out.println(identityInformationDto);
        permissionService.selectAdminAll(admin.getAdminId().toString());
        return identityInformationDto;
    }



    @Override
    public IdentityInformationDto loginUserByName(NameLoginForm nameLoginForm) throws Exception {
        Integer userId = null;
        String loginStatus = "登录成功";
        IdentityInformationDto identityInformationDto = new IdentityInformationDto();
        //判断用户是否存在
        User user = userMapper.selectOne(new QueryWrapper<User>().eq("user_name", nameLoginForm.getUserName()));
        if (user == null) {
            loginStatus = "登录失败 用户不存在";
            throw new UserException(loginStatus, 10001);
        }
        //检验密码

        if (!user.getUserPassword().equals(MD5Util.md5(nameLoginForm.getPassWord(), user.getUserPhone()))) {
            loginStatus = "登录失败 密码错误";
            throw new UserException(loginStatus, 10002);
        }
        //获取角色
        userId = user.getUserId().intValue();
        UserRole userRole = userRoleMapper.selectRoleByUserIdWithoutRoleIdThree(userId);
        Role role = roleService.getById(userRole.getRoleId());

        if (role.getRoleId().equals(2)) {
            if (mealsFeignClient.selectStoreByUserId(userId).getData() == null) {
                identityInformationDto.setStoreId(null);
            } else {
                identityInformationDto.setStoreId(String.valueOf(mealsFeignClient.selectStoreByUserId(userId).getData().getStoreId()));
            }

        }

        //签发token
        user.setUserPassword("******");
        IdentityInformationDto.User user2 = BeanUtil.toBean(user, IdentityInformationDto.User.class);
        user2.setUserPhone(user.getUserPhone());
        identityInformationDto.setUser(user2);
        identityInformationDto.setUserName(user.getUserName());
        identityInformationDto.setRoleName(role.getRoleName());

        IdentityInformationDto.User user1 = identityInformationDto.getUser();
        user1.setRoleId(userRole.getRoleId());
        user1.setRoleName(role.getRoleName());
        identityInformationDto.setUser(user1);


        UserLoginLogs userLoginLogs = new UserLoginLogs();
        userLoginLogs.setUserId(userId.toString());
        userLoginLogs.setLoginTime(LocalDateTime.now());
        userLoginLogs.setUserName(user.getUserName());
        userLoginLogs.setLoginStatus(loginStatus);
        userLoginLogsService.save(userLoginLogs);
        permissionService.selectUserAll(userId.toString());
        return identityInformationDto;
    }

    @Override
    public IdentityInformationDto loginAdminByName(NameLoginForm nameLoginForm) throws Exception {
        Integer adminId = null;
        String loginStatus = new String();
        IdentityInformationDto identityInformationDto = new IdentityInformationDto();
        //判断用户名是否存在
        Admin admin = adminMapper.selectOne(new QueryWrapper<Admin>().eq("admin_name", nameLoginForm.getUserName()));

        //检验密码
        if (!admin.getAdminPassword().equals(MD5Util.md5(nameLoginForm.getPassWord(), admin.getPhone()))) {
            loginStatus = "登录失败 密码错误";
            throw new UserException(loginStatus, 40002);
        }

        //获取角色
        adminId = admin.getAdminId().intValue();
        UserRole userRole = userRoleMapper.selectRoleByUserIdWithoutNormal(adminId);
        Role role = roleService.getById(userRole.getRoleId());


        //签发token
        admin.setAdminPassword("******");

        identityInformationDto.setUserName(admin.getAdminName());

        identityInformationDto.setRoleName(role.getRoleName());

        IdentityInformationDto.Admin admin1 = BeanUtil.toBean(admin, IdentityInformationDto.Admin.class);
        admin1.setRoleId(role.getRoleId());
        admin1.setRoleName(role.getRoleName());
        identityInformationDto.setAdmin(admin1);

        UserLoginLogs userLoginLogs = new UserLoginLogs();
        userLoginLogs.setUserId(admin.getAdminId().toString());
        userLoginLogs.setLoginTime(LocalDateTime.now());
        userLoginLogs.setUserName(admin.getAdminName());
        userLoginLogs.setLoginStatus(loginStatus);
        userLoginLogsService.save(userLoginLogs);

        System.out.println(identityInformationDto);

        permissionService.selectAdminAll(admin.getAdminId().toString());
        return identityInformationDto;
    }



    @Override
    public void sendLoginCode(String phone) throws Exception {
        if (redisUtil.hasKey(RedisKey.getSendRegCodeBySum(phone))) {
            Integer times = Integer.valueOf(redisUtil.getString(RedisKey.getSendRegCodeBySum(phone)));
            if (times >= 5) {
                throw new UserException("发送验证码过于频繁，请稍后重试",99998);
            }
        }
        //判断一分钟内是否有发送过验证码
        if (redisUtil.getString(RedisKey.getSendRegCodeByOneMin(phone)) != null) {
            throw new UserException("发送验证码时间间隔未满1min，请稍后重试",99999);
        }
        //查号码存在与否
        User user = this.getOne(new QueryWrapper<User>().eq("user_phone", phone));
        if (user == null) {
            throw new UserException("该手机号不存在",99997);
        }
        //随机六位数并调用发送短信业务
        Integer code = (int) (Math.random() * 900000) + 100000;
        String s = SMSUtil.sendCode(phone, code + "");
        if (s.equals("2")) {
            redisUtil.setString(RedisKey.getSendRegCodeInMinKey(phone), code + "", 60 * 5);
            //用于判断一分钟内是否有发送过验证码的键值对
            redisUtil.setString(RedisKey.getSendRegCodeByOneMin(phone), code + "", 60);
            //用于判断单个账号发送次数不超过5次
            if (!redisUtil.hasKey(RedisKey.getSendRegCodeBySum(phone))) {
                redisUtil.setString(RedisKey.getSendRegCodeBySum(phone), "1", 60 * 5);
            } else {
                redisUtil.increment(RedisKey.getSendRegCodeBySum(phone));
            }
        } else {
            throw new UserException("短信发送失败",99996);
        }


    }

    @Override
    public IdentityInformationDto codeLogin(String code, String phone) throws Exception {
        IdentityInformationDto identityInformationDto = new IdentityInformationDto();
        if (code.equals(redisUtil.getString(RedisKey.getSendRegCodeInMinKey(code)))) {
            if (Integer.valueOf(redisUtil.getString(redisUtil.getString(RedisKey.getSendRegCodeInMinKey(phone)))) >= 5) {
                throw new UserException("重试次数过多",99997);
            }
        }
        //删除旧判断次数的键值对
        redisUtil.delete(phone);
        //验证码判断
        try {
            if (!code.equals(redisUtil.getString(RedisKey.getSendRegCodeInMinKey(phone)))) {
                if (!redisUtil.hasKey(RedisKey.getSendRegCodeByRegistSum(phone))) {
                    redisUtil.setString(RedisKey.getSendRegCodeByRegistSum(phone), "1", 60 * 5);
                } else {
                    redisUtil.increment(RedisKey.getSendRegCodeByRegistSum(phone));
                    if (Integer.valueOf(redisUtil.getString(redisUtil.getString(RedisKey.getSendRegCodeInMinKey(phone)))) >= 5) {
                        redisUtil.del(RedisKey.getSendRegCodeInMinKey(phone));
                    }
                }
                throw new UserException("验证码不正确",98765);
            }
        }catch (Exception e){
            throw new UserException("验证码不正确",98765);
        }

        //查号码存在与否
        User user = this.getOne(new QueryWrapper<User>().eq("user_phone", phone));
        if (user == null) {
            if(adminService.getOne(new QueryWrapper<Admin>().eq("phone", phone))==null){
                throw new UserException("该手机号不存在",12345);
            }else {
                Admin admin = adminService.getOne(new QueryWrapper<Admin>().eq("phone", phone));
                //获取角色
                Integer adminId = admin.getAdminId().intValue();
                UserRole userRole = userRoleMapper.selectRoleByUserIdWithoutNormal(adminId);
                Role role = roleService.getById(userRole.getRoleId());


                //签发token
                admin.setAdminPassword("******");

                identityInformationDto.setUserName(admin.getAdminName());

                identityInformationDto.setRoleName(role.getRoleName());

                IdentityInformationDto.Admin admin1 = BeanUtil.toBean(admin, IdentityInformationDto.Admin.class);
                admin1.setRoleId(role.getRoleId());
                admin1.setRoleName(role.getRoleName());
                identityInformationDto.setAdmin(admin1);

                UserLoginLogs userLoginLogs = new UserLoginLogs();
                userLoginLogs.setUserId(admin.getAdminId().toString());
                userLoginLogs.setLoginTime(LocalDateTime.now());
                userLoginLogs.setUserName(admin.getAdminName());
                userLoginLogsService.save(userLoginLogs);

                System.out.println(identityInformationDto);

                return identityInformationDto;
            }

        }

        Integer userId = null;

        //获取角色
        userId = user.getUserId().intValue();
        UserRole userRole = userRoleMapper.selectRoleByUserIdWithoutRoleIdThree(userId);
        Role role = roleService.getById(userRole.getRoleId());

        if (role.getRoleId().equals(2)) {
            if (mealsFeignClient.selectStoreByUserId(userId).getData() == null) {
                identityInformationDto.setStoreId(null);
            } else {
                identityInformationDto.setStoreId(String.valueOf(mealsFeignClient.selectStoreByUserId(userId).getData().getStoreId()));
            }

        }

        //签发token
        user.setUserPassword("******");
        IdentityInformationDto.User user2 = BeanUtil.toBean(user, IdentityInformationDto.User.class);
        user2.setUserPhone(user.getUserPhone());
        identityInformationDto.setUser(user2);
        identityInformationDto.setUserName(user.getUserName());
        identityInformationDto.setRoleName(role.getRoleName());

        IdentityInformationDto.User user1 = identityInformationDto.getUser();
        user1.setRoleId(userRole.getRoleId());
        user1.setRoleName(role.getRoleName());
        identityInformationDto.setUser(user1);


        UserLoginLogs userLoginLogs = new UserLoginLogs();
        userLoginLogs.setUserId(userId.toString());
        userLoginLogs.setLoginTime(LocalDateTime.now());
        userLoginLogs.setUserName(user.getUserName());
        userLoginLogs.setLoginStatus("调用手机号&验证码登录");
        userLoginLogsService.save(userLoginLogs);
        permissionService.selectUserAll(userId.toString());

        return identityInformationDto;


    }


    @Override
    public void insertUserOrFixer(InsertUserOrStoreParam insertUserOrStoreParam) throws Exception {

        //判断密码是否为空
        if (insertUserOrStoreParam.getPassWord() == null) throw new UserException("密码不得为空", 40004);
        //判断身份标识是否为空
        if (insertUserOrStoreParam.getMark() == null) throw new UserException("身份标识不得为空", 40005);
        //判断账号是否重复
        User user = userMapper.selectByUserName(insertUserOrStoreParam.getUserName());
        if (user != null || adminService.getOne(new QueryWrapper<Admin>().eq("admin_name", insertUserOrStoreParam.getUserName())) != null)
            throw new UserException("用户名重复", 40006);
        //判断手机号是否重复
        if (this.getOne(new QueryWrapper<User>().eq("user_phone", insertUserOrStoreParam.getPhone())) != null|| adminService.getOne(new QueryWrapper<Admin>().eq("phone", insertUserOrStoreParam.getPhone())) != null)
            throw new UserException("手机号已被注册", 40016);
        //新增时判别学生账号还是商户账号并 同时 给予对应权限   注意记录操作
        User newUser = new User();
        newUser.setUserName(insertUserOrStoreParam.getUserName());
        newUser.setUserPassword(MD5Util.md5(insertUserOrStoreParam.getPassWord(), insertUserOrStoreParam.getPhone()));
        newUser.setUserCard(insertUserOrStoreParam.getUserCard());
        newUser.setUserState(0);
        newUser.setCreateTime(LocalDateTime.now());
        newUser.setUpdateTime(LocalDateTime.now());
        newUser.setUserPhone(insertUserOrStoreParam.getPhone());
        this.save(newUser);

        User user1 = userMapper.selectByUserName(insertUserOrStoreParam.getUserName());
        //账号权限表新增
        UserRole userRole = new UserRole();
        userRole.setUserId(user1.getUserId().toString());
        userRole.setCreateTime(LocalDateTime.now());
        userRole.setRoleId(insertUserOrStoreParam.getMark());

        userRoleService.save(userRole);


        //维修员信息表新增
        if (insertUserOrStoreParam.getMark().toString().equals("4")) {
            EmployeeInfo employeeInfo = new EmployeeInfo();
            employeeInfo.setUserId(userMapper.selectByUserName(insertUserOrStoreParam.getUserName()).getUserId().toString());
            employeeInfo.setUserName(userMapper.selectByUserName(insertUserOrStoreParam.getUserName()).getUserName());
            employeeInfo.setUserState(0);
            employeeInfo.setProfession(insertUserOrStoreParam.getProfession());
            employeeInfo.setCreateUser(insertUserOrStoreParam.getOperatorUserName());
            employeeInfo.setCreateDate(LocalDateTime.now());
            employeeInfo.setExpand1(String.valueOf(insertUserOrStoreParam.getAreaCodeId()));
            employeeInfo.setExpand2(areaCodeService.getAreaCodeById(insertUserOrStoreParam.getAreaCodeId()));
            employeeInfoService.save(employeeInfo);
        }else {
            //用户信息表新增数据
            UserInfo userInfo = new UserInfo();
            Long userId = userMapper.selectByUserName(insertUserOrStoreParam.getUserName()).getUserId();
            userInfo.setUserId(userId.toString());
            userInfo.setUserName(insertUserOrStoreParam.getUserName());
            userInfo.setUserCard(insertUserOrStoreParam.getUserCard());
            userInfo.setRoleId(insertUserOrStoreParam.getMark());
            userInfo.setAreaCodeId(insertUserOrStoreParam.getAreaCodeId());
            userInfo.setAreaCodeName(areaCodeService.getAreaCodeById(insertUserOrStoreParam.getAreaCodeId()));
            String roleName = roleService.getById(insertUserOrStoreParam.getMark()).getRoleName();
            userInfo.setRoleName(roleName);
            userInfo.setUserTag(roleName);
            userInfo.setUserState("0");
            userInfo.setCreateTime(LocalDateTime.now());
            userInfo.setUpdateTime(LocalDateTime.now());
            userInfoService.save(userInfo);
        }


//        }
    }

    @Override
    public void changePassword(ChangePassWordParam changePassWordParam) throws Exception {
        //操作人
        String operator = changePassWordParam.getOperator();
        if ("3".equals(operator) || "5".equals(operator)) {
            //管理员
            Admin admin = adminMapper.selectByAdminName(changePassWordParam.getUserName());
            //判断旧密码是否与原密码相同
            String oldPassword = adminMapper.selectByAdminName(changePassWordParam.getUserName()).getAdminPassword();
            if (!MD5Util.md5(changePassWordParam.getOldPassword(), changePassWordParam.getUserName()).equals(oldPassword))
                throw new UserException("旧密码与实际密码不一致", 40007);
            //如果操作的是管理员账号执行admin表更新（更新人和时间同步）
            admin.setAdminPassword(MD5Util.md5(changePassWordParam.getNewPassWord(), changePassWordParam.getUserName()));
            admin.setUpdataTime(LocalDateTime.now());
            adminMapper.updateAdminById(admin);
        } else {
            //用户（其他角色）
            User user = userMapper.selectByUserName(changePassWordParam.getUserName());
            //判断旧密码是否与原密码相同
            String oldPassword = userMapper.selectByUserName(changePassWordParam.getUserName()).getUserPassword();
            if (!MD5Util.md5(changePassWordParam.getOldPassword(), changePassWordParam.getUserName()).equals(oldPassword))
                throw new UserException("旧密码与实际密码不一致", 40007);
            //其他的执行user表更新（更新人和时间同步）
            user.setUserPassword(MD5Util.md5(changePassWordParam.getNewPassWord(), user.getUserPhone()));
            user.setUpdateTime(LocalDateTime.now());
            this.updateById(user);
        }


    }

    @Override
    public void rePassword(Integer userId, String adminId) throws Exception {
        //操作人
        String operator = adminId;
        //用户
        User user = userMapper.selectById(userId);
        //其他的执行user表更新（更新人和时间同步）
        user.setUserPassword(MD5Util.md5("123456", user.getUserPhone()));
        user.setUpdateTime(LocalDateTime.now());
        this.updateById(user);
    }

    @Override
    public void rePassword(String userName, String adminId) throws Exception {
        Admin admin = adminMapper.selectById(adminId);
        //其他的执行user表更新（更新人和时间同步）
        User user = this.getOne(new QueryWrapper<User>().eq("user_name", userName));
        if (user == null) {
            Admin admin1 = adminService.getOne(new QueryWrapper<Admin>().eq("admin_name", userName));
            admin1.setAdminPassword(MD5Util.md5("123456",admin.getPhone()));
            adminService.updateById(admin1);
        } else {
            user.setUserPassword(MD5Util.md5("123456", user.getUserPhone()));
            this.updateById(user);
        }

//        admin.setUpdateTime(LocalDateTime.now());
//        adminMapper.updateById(admin);

    }

    @Override
    public void insertAdmin(InsertAdminParam insertAdminParam) throws Exception {
        //判断密码是否为空
        if (insertAdminParam.getPassWord() == null) throw new UserException("密码不得为空", 40003);
        //判断用户名是否重复
        if (adminMapper.selectByAdminName(insertAdminParam.getUserName()) != null || userMapper.selectByUserName(insertAdminParam.getUserName()) != null)
            throw new UserException("用户名重复", 40003);
        //判断手机号是否重复
        if (this.getOne(new QueryWrapper<User>().eq("user_phone", insertAdminParam.getPhone())) != null||adminService.getOne(new QueryWrapper<Admin>().eq("phone", insertAdminParam.getPhone()))!=null)
            throw new UserException("手机号已被注册", 40016);

        Admin admin = new Admin();
        admin.setAdminName(insertAdminParam.getUserName());
        admin.setAdminPassword(MD5Util.md5(insertAdminParam.getPassWord(), insertAdminParam.getPhone()));
        admin.setCreateTime(LocalDateTime.now());
        admin.setUpdataTime(LocalDateTime.now());
        admin.setPhone(insertAdminParam.getPhone());
        Admin adminOp = adminMapper.selectByAdminName(insertAdminParam.getOperator());
        admin.setCreateId(adminOp.getAdminId());
        adminService.save(admin);

        //赋予权限
        UserRole userRole = new UserRole();
        userRole.setUserId(admin.getAdminId().toString());
        userRole.setRoleId(3);
        userRole.setCreateTime(LocalDateTime.now());
        userRoleService.save(userRole);

    }

    @Override
    public void unableUserByUserId(Integer userId) throws UserException {
        UserInfo userInfo = userInfoMapper.selectById(userId);
        if (userInfo.getUserState().equals(1)) throw new UserException("此状态为初始状态，无需改变", 40009);
        userInfo.setUserState("1");
        userInfoMapper.updateUserInfoById(userInfo);
    }

    @Override
    public void deleteUserByUserId(Integer userId) throws UserException {
        UserInfo userInfo = userInfoMapper.selectById(userId);
        if (userInfo.getUserState().equals(2)) throw new UserException("此状态为初始状态，无需改变", 40009);
        userInfo.setUserState("2");
        userInfoMapper.updateUserInfoById(userInfo);
    }

    @Override
    public void ableUserByUserId(Integer userId) throws UserException {
        UserInfo userInfo = userInfoMapper.selectById(userId);
        if (userInfo.getUserState().equals(0)) throw new UserException("此状态为初始状态，无需改变", 40009);
        userInfo.setUserState("0");
        userInfoMapper.updateUserInfoById(userInfo);
    }

    @Override
    public void insertStoreOwner(StoreOwnerParam storeOwnerParam) throws UserException {

        User user = userMapper.selectByUserName(storeOwnerParam.getUserName());
        if (user != null) throw new UserException("用户名重复", 40006);

        User user1 = new User();
        user1.setUserState(0);
        user1.setUserName(storeOwnerParam.getUserName());
        user1.setUserPassword(storeOwnerParam.getPassword());
        user1.setUpdateTime(LocalDateTime.now());
        user1.setCreateTime(LocalDateTime.now());
        this.save(user1);

        //进行权限分配
        UserRole userRole = new UserRole();
        userRole.setUserId(userMapper.selectByUserName(storeOwnerParam.getUserName()).getUserId().toString());
        userRole.setCreateTime(LocalDateTime.now());
        userRole.setRoleId(2);
        //账号权限表新增
        userRoleService.save(userRole);


        //用户信息表新增数据
        UserInfo userInfo = new UserInfo();
        Long userId = userMapper.selectByUserName(storeOwnerParam.getUserName()).getUserId();
        userInfo.setUserId(userId.toString());
        userInfo.setRoleId(2);
        userInfo.setRoleName("商家");
        userInfo.setUserTag("商家");
        userInfo.setUserState("0");
        userInfo.setCreateTime(LocalDateTime.now());
        userInfo.setUpdateTime(LocalDateTime.now());
        userInfoService.save(userInfo);

    }


}
